package com.oc.utils;

import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.Folder;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.Multipart;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.SupportedAttachmentPart;
import net.rim.blackberry.api.mail.TextBodyPart;
import net.rim.blackberry.api.mail.Transport;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.JPEGEncodedImage;
import net.rim.device.api.ui.component.Dialog;

public class MailUtils {

	public static void sendEmailWithAttachment(String to, String subject, Bitmap image) 
	{
		// create a multipart
		Multipart mp = new Multipart();
		// data for the content of the file


		JPEGEncodedImage jpegImage = JPEGEncodedImage.encode(image, 100);
		// data for the content of the file
		SupportedAttachmentPart sap = new SupportedAttachmentPart(mp, jpegImage
				.getMIMEType(), "x-rimdeviceimage.jpg", jpegImage.getData());

		TextBodyPart tbp = new TextBodyPart(mp, "Send Image");
		// add the file to the multipart
		mp.addBodyPart(tbp);

		mp.addBodyPart(sap);
		// create a message in the sent items folder

		Folder folders[] = Session.getDefaultInstance().getStore().list(
				Folder.SENT);
		Message message = new Message(folders[0]);

		// add recipients to the message and send
		try {
			Address toAdd = new Address(to, to);
			Address toAdds[] = new Address[1];
			toAdds[0] = toAdd;
			message.addRecipients(Message.RecipientType.TO, toAdds);
			message.setContent(mp);
			message.setSubject(subject);
			Transport.send(message);
			Dialog.inform("Card was sent to " + to);
		} catch (Exception e) {
			Dialog.inform(e.toString());
		}
	}

}
